package com.jwgf.excel;

import com.alibaba.fastjson.JSON;
import com.jwgf.common.AppContext;
import com.jwgf.group.Group;
import com.jwgf.objectbox.CbfReport;
import com.jwgf.objectbox.FaultRecord;
import com.jwgf.objectbox.GroupInfo;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.ProcessRecord;
import com.jwgf.objectbox.RedLightRecord;
import com.jwgf.objectbox.ShiftReport;
import com.jwgf.objectbox.ShiftReport_;
import com.jwgf.objectbox.StatisticRecord;
import com.jwgf.report.ShiftTimeRule;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.Report;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import io.objectbox.Box;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WriteException;

public class ShiftReport2Excel {

    public static String createExcelFile(boolean shiftReport, boolean statistic, boolean cbf) {
        Box<ShiftReport> box = ObjectBox.get().boxFor(ShiftReport.class);
        long[] historyTimes = box.query().build().property(ShiftReport_.start).distinct().findLongs();
        long[] allTimes = new long[historyTimes.length + 1];
        for (int i = 0; i < historyTimes.length; ++i) {
            allTimes[i] = historyTimes[i];
        }
        allTimes[allTimes.length - 1] = Long.parseLong(ShiftTimeRule.getInstance().getCurrentShiftStartTime());

        String machineNum = MachineParameterSharedPreference.getInstance()
                .getValue(MachineParameterSharedPreference.SYSTEM_MACHINE_NUM, "1");
        ExcelWriter excelWriter = new ExcelWriter("Report_" + machineNum);
        for (long allTime : allTimes) {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(allTime);
            String start = String.format("%04d-%02d-%02d %02d-%02d",
                    c.get(Calendar.YEAR),
                    c.get(Calendar.MONTH) + 1,
                    c.get(Calendar.DAY_OF_MONTH),
                    c.get(Calendar.HOUR_OF_DAY),
                    c.get(Calendar.MINUTE));
            if (shiftReport) {
                WritableSheet sheet = excelWriter.addSheet("shift_" + start);
                List<ShiftReport> reports = Report.getInstance().getProduction(allTime);
                String[] titles = new String[]{"产量(kg)", "生产效率(%)", "机械效率(%)", "捻接循环次数", "一次成功捻接次数",
                        "接头数", "接头数/10km", "接头数/筒纱", "切疵次数", "切疵/100km",
                        "切疵/管纱", "切疵/筒纱", "满筒数", "小车落筒成功数", "换管数", "络纱时间"};
                for (int j = 0; j < titles.length; ++j) {
                    try {
                        sheet.addCell(new Label(0, j + 1, titles[j]));
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                }
                for (int j = 0; j < reports.size(); ++j) {
                    try {
                        sheet.addCell(new Label(j + 1, 0, reports.get(j).type));
                        sheet.addCell(new Number(j + 1, 1, reports.get(j).production));
                        sheet.addCell(new Number(j + 1, 2, reports.get(j).runEfficiency));
                        sheet.addCell(new Number(j + 1, 3, reports.get(j).mechanicalEfficiency));
                        sheet.addCell(new Number(j + 1, 4, reports.get(j).spliceCount));
                        sheet.addCell(new Number(j + 1, 5, reports.get(j).splicesCountRightFirstTime));
                        sheet.addCell(new Number(j + 1, 6, reports.get(j).yarnJointCount));
                        sheet.addCell(new Number(j + 1, 7, reports.get(j).yarnJointCountPer10Km));
                        sheet.addCell(new Number(j + 1, 8, reports.get(j).yarnJointCountPerPackage));
                        sheet.addCell(new Number(j + 1, 9, reports.get(j).cutCount));
                        sheet.addCell(new Number(j + 1, 10, reports.get(j).cutCountPer10Km));
                        sheet.addCell(new Number(j + 1, 11, reports.get(j).cutCountPerCop));
                        sheet.addCell(new Number(j + 1, 12, reports.get(j).cutCountPerPackage));
                        sheet.addCell(new Number(j + 1, 13, reports.get(j).packageCount));
                        sheet.addCell(new Number(j + 1, 14, reports.get(j).succeedDoffingPackageCount));
                        sheet.addCell(new Number(j + 1, 15, reports.get(j).copChangeCount));
                        sheet.addCell(new Number(j + 1, 16, reports.get(j).windingTime / 1000));
//                        sheet.addCell(new Number(j + 1, 16, ((long) (Math.random() * 3600 * 8))));
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }

                }
            }
            if (statistic) {
                WritableSheet sheet = excelWriter.addSheet("statistic_" + start);
                List<StatisticRecord> statistics = Report.getInstance().getStatistic(allTime);
                String[] titles = new String[]{"打结总数", "打结总时间", "打结平均时间",
                        "落筒总数", "落筒总时间", "落筒平均时间",
                        "红灯总数", "红灯总时间", "红灯平均时间",
                        "工艺总数", "工艺总时间", "工艺平均时间",
                        "故障总数", "故障总时间", "故障平均时间"};
                for (int j = 0; j < titles.length; ++j) {
                    try {
                        sheet.addCell(new Label(0, j + 1, titles[j]));
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                }
                GroupInfo groupInfo = Report.getInstance().getGroupInfo(statistics.get(0).start);
                List<Group> groups = JSON.parseArray(groupInfo.groupInfo, Group.class);
                int groupCnt = groups.size();
                int spindleCnt = 0;
                int count = 0;
                for (Group group : groups) {
                    count += group.getSpindles().size();
                }
                spindleCnt = count;
                ArrayList<String> listLabel = new ArrayList<>();
                listLabel.add(AppContext.getInstance().getResources().getString(R.string.all));
                listLabel.add(AppContext.getInstance().getResources().getString(R.string.average));
                for (int i = 1; i <= groupCnt; ++i) {
                    listLabel.add(AppContext.getInstance().getResources().getString(R.string.group) + i);
                }
                for (int i = 1; i <= spindleCnt; ++i) {
                    listLabel.add("" + i);
                }
                for (int j = 0; j < statistics.size(); ++j) {
                    try {
                        sheet.addCell(new Label(j + 1, 0, listLabel.get(j)));
                        sheet.addCell(new Label(j + 1, 1, String.format("%.2f", statistics.get(j).spliceCount)));
                        sheet.addCell(new Label(j + 1, 2, String.valueOf(statistics.get(j).spliceTime)));
                        sheet.addCell(new Label(j + 1, 3, String.valueOf(statistics.get(j).spliceAvgTime)));
                        sheet.addCell(new Label(j + 1, 4, String.format("%.2f", statistics.get(j).packageCount)));
                        sheet.addCell(new Label(j + 1, 5, String.valueOf(statistics.get(j).dofferTime)));
                        sheet.addCell(new Label(j + 1, 6, String.valueOf(statistics.get(j).dofferAvgTime)));
                        sheet.addCell(new Label(j + 1, 7, String.format("%.2f", statistics.get(j).redLightCount)));
                        sheet.addCell(new Label(j + 1, 8, String.valueOf(statistics.get(j).redLightTime)));
                        sheet.addCell(new Label(j + 1, 9, String.valueOf(statistics.get(j).redLightAvgTime)));
                        sheet.addCell(new Label(j + 1, 10, String.format("%.2f", statistics.get(j).processCount)));
                        sheet.addCell(new Label(j + 1, 11, String.valueOf(statistics.get(j).processTime)));
                        sheet.addCell(new Label(j + 1, 12, String.valueOf(statistics.get(j).processAvgTime)));
                        sheet.addCell(new Label(j + 1, 13, String.format("%.2f", statistics.get(j).faultCount)));
                        sheet.addCell(new Label(j + 1, 14, String.valueOf(statistics.get(j).faultTime)));
                        sheet.addCell(new Label(j + 1, 15, String.valueOf(statistics.get(j).faultAvgTime)));
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }

                }
            }

            if (cbf) {
                WritableSheet sheet = excelWriter.addSheet("cbf_" + start);
                CbfReport cbfReport = Report.getInstance().getCurrentCBFDoffer(allTime);
                String[] cbf_d_titles = new String[]{"进管数", "空管数", "残纱数", "进入单锭数", "空管率", "残纱率",
                        "第一生头通过数", "第一生头成功数", "第一生头成功率",
                        "第二生头通过数", "第二生头成功数", "第二生头成功率",
                        "第三生头通过数", "第三生头成功数", "第三生头成功率",
                        "拔管处通过数"};

                for (int j = 0; j < cbf_d_titles.length; ++j) {
                    try {
                        sheet.addCell(new Label(0, j + 1, cbf_d_titles[j]));
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    sheet.addCell(new Label(1, 0, "大纱库"));
                    sheet.addCell(new Number(1, 1, cbfReport.enterTubeCountD));
                    sheet.addCell(new Number(1, 2, cbfReport.emptyTubeCountD));
                    sheet.addCell(new Number(1, 3, cbfReport.remnantTubeCountD));
                    sheet.addCell(new Number(1, 4, cbfReport.enterTubeCountD));
                    sheet.addCell(new Number(1, 5, cbfReport.emptyTubeRatioD));
                    sheet.addCell(new Number(1, 6, cbfReport.remnantTubeRatioD));
                    sheet.addCell(new Number(1, 7, cbfReport.joint1PassCountD));
                    sheet.addCell(new Number(1, 8, cbfReport.joint1SucceedCountD));
                    sheet.addCell(new Number(1, 9, cbfReport.joint1SucceedRatioD));
                    sheet.addCell(new Number(1, 10, cbfReport.joint2PassCountD));
                    sheet.addCell(new Number(1, 11, cbfReport.joint2SucceedCountD));
                    sheet.addCell(new Number(1, 12, cbfReport.joint2SucceedRatioD));
                    sheet.addCell(new Number(1, 13, cbfReport.joint3PassCountD));
                    sheet.addCell(new Number(1, 14, cbfReport.joint3SucceedCountD));
                    sheet.addCell(new Number(1, 15, cbfReport.joint3SucceedRatioD));
                    sheet.addCell(new Number(1, 16, cbfReport.pullTubeCount));
                } catch (WriteException e) {
                    e.printStackTrace();
                }

                String[] cbf_x_titles = new String[]{"进管数", "空管数", "残纱数", "空管率", "残纱率",
                        "一通道生头成功数", "一通道生头失败数", "一通道生头成功率",
                        "二通道生头成功数", "二通道生头失败数", "二通道生头成功率"};

                for (int j = 0; j < cbf_x_titles.length; ++j) {
                    try {
                        sheet.addCell(new Label(2, j + 1, cbf_x_titles[j]));
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    sheet.addCell(new Label(3, 0, "细络联"));
                    sheet.addCell(new Number(3, 1, cbfReport.enterTubeCountX));
                    sheet.addCell(new Number(3, 2, cbfReport.emptyTubeCountX));
                    sheet.addCell(new Number(3, 3, cbfReport.remnantTubeCountX));
                    sheet.addCell(new Number(3, 4, cbfReport.emptyTubeRatioX));
                    sheet.addCell(new Number(3, 5, cbfReport.remnantTubeRatioX));
                    sheet.addCell(new Number(3, 6, cbfReport.channel1SucceedCountX));
                    sheet.addCell(new Number(3, 7, cbfReport.channel1FailedCountX));
                    sheet.addCell(new Number(3, 8, cbfReport.channel1SucceedRatioX));
                    sheet.addCell(new Number(3, 9, cbfReport.channel2SucceedCountX));
                    sheet.addCell(new Number(3, 10, cbfReport.channel2FailedCountX));
                    sheet.addCell(new Number(3, 11, cbfReport.channel2SucceedRatioX));
                } catch (WriteException e) {
                    e.printStackTrace();
                }

                String[] ad_titles = new String[]{"小车落筒成功个数", "小车落筒个数", "小车落筒成功率", "总满筒个数", "总落筒成功率"};

                for (int j = 0; j < ad_titles.length; ++j) {
                    try {
                        sheet.addCell(new Label(4, j + 1, ad_titles[j]));
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    sheet.addCell(new Label(5, 0, "小车"));
                    sheet.addCell(new Number(5, 1, cbfReport.dofferDoffSucceedCount));
                    sheet.addCell(new Number(5, 2, cbfReport.dofferDoffCount));
                    sheet.addCell(new Number(5, 3, cbfReport.dofferSucceedRatio));
                    sheet.addCell(new Number(5, 4, cbfReport.doffCount));
                    sheet.addCell(new Number(5, 5, cbfReport.doffSucceedRatio));
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }


        }
        excelWriter.close();
        return excelWriter.getFilePath();

    }
}
